<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        body {
            background-color: #252525;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #ffffde;
            gap: 0 12px;
        }

        .container {
            background-color: inherit;
            position: relative;
            display: flex;
            align-items: center;
            flex-direction: column;
            overflow: hidden;
            width: 100px;
            height: 300px;
            mask-image: linear-gradient(180deg,
                    transparent,
                    #fff 20%,
                    #fff 80%,
                    transparent);
        }

        .container div {
            white-space: nowrap;
            display: flex;
            flex-direction: column;
            animation: scrollUp var(--t) linear infinite;
            user-select: none;
        }

        .container div:nth-child(2) {
            animation: scrollUp2 var(--t) linear infinite;
            animation-delay: calc(var(--t) / -2);
            user-select: none;
        }

        @keyframes scrollUp {
            from {
                transform: translateY(100%);
            }

            to {
                transform: translateY(-100%);
            }
        }

        @keyframes scrollUp2 {
            from {
                transform: translateY(0%);
            }

            to {
                transform: translateY(-200%);
            }
        }

        .container div span {
            display: inline-flex;
            background-color: #333;
            letter-spacing: 0.2em;
            margin: 10px;
            border-radius: 5px;
            padding: 5px 10px;
        }
    </style>
</head>

<body>
    <div class="container" style="--t: 16s">
        <div>
            <span>Lorem1</span>
            <span>Lorem2</span>
            <span>Lorem4</span>
            <span>Lorem5</span>
            <span>Lorem6</span>
            <span>Lorem7</span>
            <span>Lorem8</span>
            <span>Lorem9</span>
        </div>
        <div>
            <span>Lorem1</span>
            <span>Lorem2</span>
            <span>Lorem4</span>
            <span>Lorem5</span>
            <span>Lorem6</span>
            <span>Lorem7</span>
            <span>Lorem8</span>
            <span>Lorem9</span>
        </div>
    </div>

    <div class="container" style="--t: 12s">
        <div>
            <span>Lorem1</span>
            <span>Lorem2</span>
            <span>Lorem4</span>
            <span>Lorem5</span>
            <span>Lorem6</span>
            <span>Lorem7</span>
            <span>Lorem8</span>
            <span>Lorem9</span>
        </div>
        <div>
            <span>Lorem1</span>
            <span>Lorem2</span>
            <span>Lorem4</span>
            <span>Lorem5</span>
            <span>Lorem6</span>
            <span>Lorem7</span>
            <span>Lorem8</span>
            <span>Lorem9</span>
        </div>
    </div>

    <div class="container" style="--t: 8s">
        <div>
            <span>Lorem1</span>
            <span>Lorem2</span>
            <span>Lorem4</span>
            <span>Lorem5</span>
            <span>Lorem6</span>
            <span>Lorem7</span>
            <span>Lorem8</span>
            <span>Lorem9</span>
        </div>
        <div>
            <span>Lorem1</span>
            <span>Lorem2</span>
            <span>Lorem4</span>
            <span>Lorem5</span>
            <span>Lorem6</span>
            <span>Lorem7</span>
            <span>Lorem8</span>
            <span>Lorem9</span>
        </div>
    </div>

    <div class="container" style="--t: 4s">
        <div>
            <span>Lorem1</span>
            <span>Lorem2</span>
            <span>Lorem4</span>
            <span>Lorem5</span>
            <span>Lorem6</span>
            <span>Lorem7</span>
            <span>Lorem8</span>
            <span>Lorem9</span>
        </div>
        <div>
            <span>Lorem1</span>
            <span>Lorem2</span>
            <span>Lorem4</span>
            <span>Lorem5</span>
            <span>Lorem6</span>
            <span>Lorem7</span>
            <span>Lorem8</span>
            <span>Lorem9</span>
        </div>
    </div>

    <script>
        const containers = document.querySelectorAll(".container");

        containers.forEach((container) => {
            const divs = container.querySelectorAll("div");
            let isDragging = false;
            let startY, initialTranslateY = 0;

            // 停止拖动的通用方法
            function stopDragging() {
                isDragging = false;
                divs.forEach((div) => {
                    div.style.transition = ""; // 恢复动画平滑过渡
                });
                container.style.cursor = 'grab';
            }

            // 鼠标进入暂停动画
            container.addEventListener("mouseenter", () => {
                divs.forEach((div) => {
                    div.style.animationPlayState = "paused";
                });
                container.style.cursor = 'grab';
            });

            // 鼠标移开恢复动画并移除事件监听
            container.addEventListener("mouseleave", () => {
                divs.forEach((div) => {
                    div.style.animationPlayState = "running";
                });
                stopDragging(); // 确保停止拖动
                container.removeEventListener('mousemove', moveFunc);
            });

            // 计算拖动
            function moveFunc(e) {
                if (isDragging) {
                    // const deltaY = e.pageY - startY; // 计算鼠标移动的距离
                    // const containerHeight = container.clientHeight; // 获取父容器的高度
                    // const translateYPercentage = ((initialTranslateY + deltaY) / containerHeight) * 100; // 计算相对父容器高度的百分比

                    // // 更新每个div的transform属性
                    // divs.forEach((div) => {
                    //     div.style.transform = `translateY(${translateYPercentage}%)`;
                    // });


                    const deltaY = e.pageY - startY; // 计算鼠标移动的距离
        const containerHeight = container.clientHeight; // 获取父容器的高度
        const currentDivTop = parseFloat(divs[0].style.transform.replace('translateY(', '').replace('%', '')) || 0; // 获取当前div顶部的位移
        const translateYPercentage = ((currentDivTop + deltaY) / containerHeight) * 100; // 计算相对父容器高度的百分比

        // 更新每个div的transform属性
        divs.forEach((div) => {
            div.style.transform = `translateY(${translateYPercentage}%)`;
        });
                }
            }

            // 鼠标按下，开始拖动
            container.addEventListener('mousedown', (e) => {
                isDragging = true;
                startY = e.pageY;
                initialTranslateY = parseFloat(divs[0].style.transform.replace('translateY(', '').replace('px)', '')) || 0; // 获取当前位移
                container.style.cursor = 'grabbing';

                divs.forEach((div) => {
                    div.style.animation = "initial !important";
                    div.style.transition = "none"; // 暂停动画的平滑过渡
                    div.style.animationPlayState = "paused";
                });

                container.addEventListener('mousemove', moveFunc);
            });

            // 鼠标抬起，停止拖动并恢复动画
            container.addEventListener('mouseup', () => {
                stopDragging();

                // 恢复动画
                divs.forEach((div) => {
                    div.style.transition = "transform var(--t) linear"; // 添加过渡
                    div.style.transform = div.style.transform; // 应用当前位移
                });
            });

            // 鼠标离开窗口时停止拖动
            container.addEventListener('mouseup', () => {
                stopDragging();

                divs.forEach((div) => {
                    // div.style.transition = "transform var(--t) linear"; // 添加过渡
                    // div.style.transform = div.style.transform; // 应用当前位移
                    console.log(div.style.transform);
                });
            });
        });
    </script>
</body>

</html>